package com.code.zg.rxapplication.utils;

import com.code.zg.rxapplication.bean.User;
import com.code.zg.rxapplication.bean.UsersResponse;

import java.util.List;

import retrofit.RestAdapter;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by zhanggang on 2017/9/13.
 */

public class SeApiManager {
    private final StackExchangeService stackExchangeService;

    public SeApiManager() {
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint("https://api.stackexchange.com")
                .setLogLevel(RestAdapter.LogLevel.BASIC)
                .build();

        stackExchangeService = restAdapter.create(StackExchangeService.class);
    }

    public Observable<List<User>>getTenMostPopularSOusers(){
        return stackExchangeService.getTenMostPopularSOusers()
                .map(new Func1<UsersResponse, List<User>>() {
                    @Override
                    public List<User> call(UsersResponse usersResponse) {

                     return usersResponse.getUsers();
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
    public Observable<List<User>>getMostPopularSOusers(int howmany){
        return stackExchangeService.getMostPopularSOusers(howmany)
                .map(new Func1<UsersResponse, List<User>>() {
                    @Override
                    public List<User> call(UsersResponse usersResponse) {
                        return usersResponse.getUsers();
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
    public  void test(){
        stackExchangeService.getMostPopularSOusers(10).subscribe(new Subscriber<UsersResponse>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(UsersResponse usersResponse) {

            }
        });
    }
}
